package com.pjhouse.module.action.user;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.citrus.service.uribroker.URIBrokerService;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.TurbineRunData;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.google.common.base.Strings;
import com.pjhouse.core.common.domain.Result;
import com.pjhouse.core.user.domain.UserDTO;
import com.pjhouse.core.user.service.UserService;

public class UserAction {
	
	@Resource
	private UserService userService;
	@Resource
	protected URIBrokerService uriBrokerService;

	public void doAddUser(
			@Param("phone") final String phone,
			@Param("password") final String password,
			@Param("role") final String role,
			@Param("status") final int status,
			@Param("name") final String name,
			@Param("memo") final String memo,
			HttpServletResponse response,
			Context context, TurbineRunData rundata){
		if(Strings.isNullOrEmpty(phone)){
			context.put("message", "请输入账号");
			return ;
		}
		context.put("phone", phone);
		if(Strings.isNullOrEmpty(password)){
			context.put("message", "请输入密码");
			return ;
		}
		context.put("password", password);
		if(Strings.isNullOrEmpty(role)){
			context.put("message", "请选择权限");
			return ;
		}
		context.put("role", role);
		if(Strings.isNullOrEmpty(name)){
			context.put("message", "请输入名字");
			return ;
		}
		context.put("name", name);
		UserDTO user = new UserDTO();
		user.setContractDate(new Date());
		user.setMemo(memo);
		user.setPhone(phone);
		user.setPassword(password);
		user.setRole(role);
		user.setStatus(status);
		user.setName(name);
		Result<Long> r = userService.addUser(user);
		if(r.isFailure()){
			context.put("message", r.getErrorMessage());
			return ;
		}
		rundata.setRedirectLocation(uriBrokerService.getURIBroker("userModule").render() + "/userList.htm");
	}
	
	
	public void doModifyUser(
			@Param("id") final long id,
			@Param("phone") final String phone,
			@Param("password") final String password,
			@Param("role") final String role,
			@Param("status") final int status,
			@Param("name") final String name,
			@Param("memo") final String memo,
			HttpServletResponse response,
			Context context, TurbineRunData rundata){
		if(id <= 0){
			context.put("message", "参数错误");
			return ;
		}
		if(Strings.isNullOrEmpty(phone)){
			context.put("message", "请输入账号");
			return ;
		}
		context.put("phone", phone);
		if(Strings.isNullOrEmpty(password)){
			context.put("message", "请输入密码");
			return ;
		}
		context.put("password", password);
		if(Strings.isNullOrEmpty(role)){
			context.put("message", "请选择权限");
			return ;
		}
		context.put("role", role);
		if(Strings.isNullOrEmpty(name)){
			context.put("message", "请输入名字");
			return ;
		}
		context.put("name", name);
		Result<UserDTO> r = userService.getById(id);
		if(r.isFailure()){
			context.put("message", r.getErrorMessage());
			return ;
		}
		UserDTO user = r.getValue();
		if(user == null){
			context.put("message", "对应账号不存在");
			return ;
		}
		user.setContractDate(new Date());
		user.setMemo(memo);
		user.setPhone(phone);
		user.setPassword(password);
		user.setRole(role);
		user.setStatus(status);
		user.setName(name);
		Result<Long> result = userService.addUser(user);
		if(result.isFailure()){
			context.put("message", r.getErrorMessage());
			return ;
		}
		rundata.setRedirectLocation(uriBrokerService.getURIBroker("userModule").render() + "/userList.htm");
	}
}
